package ji.sky.accountdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    private RadioGroup rgSelect;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private MainAccountFragment mainAccountFragment;
    private MainRecordFragment mainRecordFragment;
    private MainChartFragment mainChartFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainAccountFragment = new MainAccountFragment();
        mainRecordFragment = new MainRecordFragment();
        mainChartFragment = new MainChartFragment();

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.fy_main,mainAccountFragment);
        transaction.add(R.id.fy_main,mainRecordFragment);
        transaction.add(R.id.fy_main,mainChartFragment);
        transaction.show(mainAccountFragment);
        transaction.hide(mainRecordFragment);
        transaction.hide(mainChartFragment);
        transaction.commit();
        rgSelect = findViewById(R.id.rg_select);
        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                transaction = manager.beginTransaction();
                switch (i){
                    case R.id.rb_account:
                        transaction.show(mainAccountFragment);
                        transaction.hide(mainRecordFragment);
                        transaction.hide(mainChartFragment);
                        break;
                    case R.id.rb_write:
                        transaction.hide(mainAccountFragment);
                        transaction.show(mainRecordFragment);
                        transaction.hide(mainChartFragment);
                        break;
                    case R.id.rb_chart:
                        transaction.hide(mainAccountFragment);
                        transaction.hide(mainRecordFragment);
                        transaction.show(mainChartFragment);
                        break;
                }
                transaction.commit();
            }
        });
    }
}